package com.wangsd.web.api.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.shop.entity.CouponOrder;
import com.wangsd.web.shop.entity.Merchant;
import com.wangsd.web.shop.service.CouponOrderService;
import com.wangsd.web.shop.service.CouponService;
import com.wangsd.web.shop.service.MerchantService;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "核销接口管理")
@RestController
@RequestMapping("/api/consumer")
@Slf4j
public class MiniConsumerController extends MyController {
    @Autowired
    CouponService couponService;
    @Autowired
    CouponOrderService couponOrderService;
    @Autowired
    SysUserService userService;
    @Autowired
    MerchantService merchantService;

    /**
     * 商户登陆
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/login")
    public Result login(String username, String password) {
        SysUser user = userService.selectByLogin(username, password);
        if (user.getType() != 4) {
            throw new BusinessException("账号密码错误");
        }
        return Result.success(user.getParentCode());
    }

    /**
     * 查询已核销的数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectCouponOrder")
    public Result selectCouponOrder(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                                    @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize) {
        String merchantCode = request.getHeader("merchantCode");
        CouponOrder queryObj = new CouponOrder();
        queryObj.setStatus(2);
        queryObj.setStockType("merNORMAL");
        queryObj.setParentCode(merchantCode);
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<CouponOrder> list = couponOrderService.selectPage(queryObj, "consumeTime", "descend", null);
        return Result.success(getPageInfo(page, list));
    }


    /**
     * 核销商家券
     * @param couponCode
     * @return
     */
    @GetMapping("/useMerCoupon")
    public Result useMerCoupon(String couponCode) {
        String merchantCode = request.getHeader("merchantCode");
        CouponOrder couponOrder = couponOrderService.selectByCouponCode(couponCode);
        if (!merchantCode.equals(couponOrder.getParentCode())) {
            throw new BusinessException("无权限核销该券");
        }
        couponService.useMerCoupon(couponCode, merchantCode);
        return Result.success("核销成功");
    }
}
